package com.ug.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ug.common.constant.SecurityConstants;
import com.ug.common.response.ResponseMessage;
import com.ug.common.response.ResponseMessageFactory;
import com.ug.common.utils.ExcelUtil;
import com.ug.common.utils.JwtUtil;
import com.ug.common.utils.StringUtil;
import com.ug.logs.annotation.Log;
import com.ug.system.entity.SysUser;
import com.ug.system.entity.dto.QueryDto;
import com.ug.system.entity.vo.SysUserExportVO;
import com.ug.system.entity.vo.SysUserVO;
import com.ug.system.entity.dto.PasswordDto;
import com.ug.system.entity.dto.SysUserDto;
import com.ug.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author LY
 * @create 2024/7/17 10:07
 * @Description 用户控制层
 */
@Tag(name = "SysUserController", description = "用户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/user")
public class SysUserController {

    private final SysUserService userService;

    /**
     * 查询用户列表
     * @param sysUserDto 请求查询信息入参
     * @param queryDto 请求查询分页信息入参
     */
    @Operation(summary = "用户列表", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("查询用户列表")
    @GetMapping("/table")
    public ResponseMessage<IPage<SysUserVO>> queryUserTable(SysUserDto sysUserDto, QueryDto queryDto){
        return ResponseMessageFactory.getSuccessResponseMessage(userService.queryUserTable(sysUserDto, queryDto));
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "用户详情", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("获取用户详情")
    @GetMapping("/info")
    public ResponseMessage<SysUser> findSysUser(){
        return ResponseMessageFactory.getSuccessResponseMessage(userService.findByUserId(JwtUtil.getUserId()));
    }

    /**
     * 编辑用户
     * @param sysUserVO 请求用户信息入参
     */
    @Operation(summary = "编辑用户", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("编辑用户")
    @PostMapping("/edit")
    public ResponseMessage<String> editUser(@RequestBody SysUserVO sysUserVO){
        String str = StringUtil.getEditType(sysUserVO.getUserId());
        userService.editUser(sysUserVO);
        return ResponseMessageFactory.getSuccessResponseMessage(str);
    }

    /**
     * 删除用户
     * @param userId 请求用户id入参
     */
    @Operation(summary = "删除用户", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("删除用户")
    @DeleteMapping("/del")
    public ResponseMessage<String> delUser(@Parameter(description = "用户userId") @RequestParam("userId") String userId){
        userService.delUser(userId);
        return ResponseMessageFactory.getSuccessResponseMessage("删除成功");
    }

    /**
     * 修改用户状态
     * @param userId 字典编号
     * @param enabled true:启用 false:停用
     */
    @Operation(summary = "改变用户状态", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("改变用户状态")
    @PutMapping("/enabled")
    public ResponseMessage<String> enabledUser(@RequestParam("userId") Long userId, @RequestParam("enabled") Integer enabled){
        String str = enabled == 1 ? "启用" : "停用";
        userService.enabledUser(userId, enabled);
        return ResponseMessageFactory.getSuccessResponseMessage(str + "成功");
    }

    /**
     * 修改用户密码
     * @param passwordDto 请求修改密码对象入参
     */
    @Operation(summary = "修改用户密码", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log(value = "修改用户密码")
    @PutMapping("/password")
    public ResponseMessage<String> updatePassword(@RequestBody PasswordDto passwordDto){
        userService.updatePassword(passwordDto);
        return ResponseMessageFactory.getSuccessResponseMessage("修改成功");
    }

    /**
     * 为使用快捷登录提供账号密码添加服务
     * @param username 用户名
     * @param password 密码
     */
    @Operation(summary = "添加账号密码", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log(value = "添加账号密码")
    @PutMapping("/saveAccount")
    public ResponseMessage<String> saveAccount(@RequestParam("username") String username, @RequestParam("password") String password){
        userService.saveAccount(username, password);
        return ResponseMessageFactory.getSuccessResponseMessage("添加成功");
    }

    /**
     * 修改昵称和邮箱
     * @param userDto 用户信息
     */
    @Operation(summary = "修改用户信息", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log(value = "修改用户信息")
    @PutMapping("/updateSysUser")
    public ResponseMessage<String> updateSysUser(SysUserDto userDto){
        userService.updateSysUser(userDto);
        return ResponseMessageFactory.getSuccessResponseMessage("修改成功");
    }

    /**
     * 导出用户信息
     */
    @Hidden
    @Log(value = "导出用户信息")
    @PostMapping("/export")
    public void userExport(@RequestBody SysUserDto sysUserDto, HttpServletResponse response) {
        List<SysUserExportVO> list = userService.exportList(sysUserDto);
        try {
            ExcelUtil.exportExcel(list, "用户数据", SysUserExportVO.class, response);
        }catch (Exception e){
            throw new RuntimeException("导出失败：" + e.getMessage());
        }
    }
}
